package com.example.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {


    @Autowired
    RedisTemplate redisTemplate;




    @Override
    public Object getKey(String key) {

        try {
            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            return operations.get(key);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void setExpire(String key, String value, long expire) {
        try {
            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expire, TimeUnit.MILLISECONDS);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public boolean delete(String key) {
        return redisTemplate.delete(key);


    }

    @Override
    public boolean setMap(Map<String, Object> map,String mapName) {


        Set<String> set = map.keySet();
        Iterator<String> iterator = set.iterator();

        while (iterator.hasNext()){
            String key = iterator.next();
            redisTemplate.opsForHash().put(mapName,key,map.get(key));
        }




        return true;
    }

    @Override
    public boolean expire(String key, int expireTime) {

        redisTemplate.expire(key, expireTime, TimeUnit.MILLISECONDS);


        return false;
    }
}
